<?php

namespace App\Store\Controllers\Basic;

use App\Store\Models\Member\Member;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Store\Models\Member\SiteMemberLv;
use App\Common\Services\Store\Common as StoreCommon;
use App\Store\Validate\Member\SiteMemberLv as ValidateMemberSiteMemberLv;

/**
 * 会员等级信息
 */
class SiteMemberLvController extends Controller
{
    /**
     * 获取列表
     */
    public function getlist(Request $request)
    {
        $list = SiteMemberLv::getList($request);
        return response()->json(['code' => '000', 'msg' => '获取成功', 'data' => $list]);
    }

    /**
     * 获取详情
     */
    public function getinfo(Request $request)
    {
        $id = $request->id ?? 0;
        $cp_id = StoreCommon::getCurrentAccountCompanyId($request);
        $info = SiteMemberLv::where(['id' => $id, 'cp_id' => $cp_id, 'is_del' => 1])->first();
        if (!$info) {
            return response()->json(['code' => '001', 'msg' => '参数错误']);
        }
        return response()->json(['code' => '000', 'msg' => '获取成功', 'data' => ['info' => $info]]);
    }

    /**
     * 新增
     */
    public function dopostadd(Request $request)
    {
        $req_data = $request->only(['lv_name', 'init_points_num', 'remark']);

        $Validate = new ValidateMemberSiteMemberLv();
        if (!$Validate->check($req_data)) {
            return response()->json(['code' => '001', 'msg' => $Validate->getError()]);
        }
        $accountinfo= StoreCommon::getCurrentAccountinfo($request);
        $isexist=SiteMemberLv::where([['lv_name', $req_data['lv_name']], ['cp_id',$accountinfo['cp_id']],['is_del',1]])->first();
        if($isexist){
            return response()->json(['code' => '001', 'msg' => '等级名称不允许重复']);
        }
        $data = $req_data;
        $data['create_account_id'] = $accountinfo['id'];
        $data['update_account_id'] = $accountinfo['id'];
        $data['create_account_real_name'] = $accountinfo['real_name'];
        $data['update_account_real_name'] = $accountinfo['real_name'];
        $data['cp_id'] = $accountinfo['cp_id'];
        $data['create_at'] = time();
        $data['update_at'] = time();
        SiteMemberLv::create($data);
        return response()->json(['code' => '000', 'msg' => '新增成功']);
    }

    /**
     * 编辑
     */
    public function dopostedit(Request $request)
    {
        $req_data = $request->only(['id', 'lv_name', 'init_points_num', 'remark']);

        $Validate = new ValidateMemberSiteMemberLv('edit');
        if (!$Validate->check($req_data)) {
            return response()->json(['code' => '001', 'msg' => $Validate->getError()]);
        }
        $accountinfo= StoreCommon::getCurrentAccountinfo($request);
        $cp_id = $accountinfo['cp_id'];

        $info = SiteMemberLv::where([['id', $req_data['id']], ['cp_id', $cp_id], ['is_del', 1]])->first();
        if (!$info) {
            return response()->json(['code' => '001', 'msg' => '参数错误']);
        }
        $isexist=SiteMemberLv::where([['id','<>',$req_data['id']],['lv_name', $req_data['lv_name']], ['cp_id',$accountinfo['cp_id']],['is_del',1]])->first();
        if($isexist){
            return response()->json(['code' => '001', 'msg' => '等级名称不允许重复']);
        }

        $data = $req_data;
        unset($data['id']);
        $data['update_at'] = time();
        $data['update_account_id'] = $accountinfo['id'];
        $data['update_account_real_name'] = $accountinfo['real_name'];
        SiteMemberLv::where(['id' => $req_data['id']])->update($data);
        return response()->json(['code' => '000', 'msg' => '编辑成功']);
    }


    /**
     * 删除
     */
    public function del(Request $request)
    {
        $id = $request->id ?? 0;
        $cp_id = StoreCommon::getCurrentAccountCompanyId($request);
        $info = SiteMemberLv::query()->where(['id' => $id, 'cp_id' => $cp_id, 'is_del' => 1])->first();
        if (empty($info)) {
            return response()->json(['code' => '001', 'msg' => '会员数据不存在']);
        }
        $memIds  = Member::query()
            ->where('cp_id',$cp_id)
            ->where('is_del',1)
            ->where('member_lv',$id)
            ->pluck('id')
            ->toArray();

        $data['is_del']=2;
        $data['update_at']=time();
        SiteMemberLv::query()
            ->where(['id' => $id, 'cp_id' => $cp_id])
            ->update($data);

        if(!empty($memIds)){
            Member::query()
                ->where('cp_id',$cp_id)
                ->where('is_del',1)
                ->whereIn('id',$memIds)
                ->update(['member_lv'=>0]);
        }
        return response()->json(['code' => '000', 'msg' => '删除成功']);
    }
}
